"""
生成折线图图表
"""
import json
from pyecharts.charts import Line
from pyecharts.options import TitleOpts
from pyecharts.options import LabelOpts

file_path = "/Users/dongjian/Documents/dev_file/"
# 从文件中读取数据
f_us = open(file_path + "美国.txt", "r", encoding="UTF-8")
us_data = f_us.read()

f_jp = open(file_path + '日本.txt', "r", encoding="UTF-8")
jp_data = f_jp.read()

f_in = open(file_path + '印度.txt', "r", encoding="UTF-8")
in_data = f_in.read()
# print(us_data)
# 去掉文件中不符合json格式的开头和结尾
us_data = us_data.replace("jsonp_1629344292311_69436(", "")
us_data = us_data[:-2]

jp_data = jp_data.replace("jsonp_1629350871167_29498(", "")
jp_data = jp_data[:-2]

in_data = in_data.replace("jsonp_1629350745930_63180(", "")
in_data = in_data[:-2]
# JSON字符串转字典
us_dict = json.loads(us_data)
jp_dict = json.loads(jp_data)
in_dict = json.loads(in_data)
# print(type(us_dict))
# print(us_dict)
# 获取x轴数据
us_trend_data = us_dict["data"][0]["trend"]
us_x_data = us_trend_data["updateDate"][:314]

jp_trend_data = jp_dict["data"][0]["trend"]
jp_x_data = jp_trend_data["updateDate"][:314]

in_trend_data = in_dict["data"][0]["trend"]
in_x_data = in_trend_data["updateDate"][:314]

# 获取轴数据
us_y_data = us_trend_data["list"][0]["data"][:314]
jp_y_data = jp_trend_data["list"][0]["data"][:314]
in_y_data = in_trend_data["list"][0]["data"][:314]
# 生成图表
# 构建折线图对象
line = Line()

line.add_xaxis(us_x_data)
line.add_yaxis("美国确诊人数", us_y_data, label_opts=LabelOpts(is_show=False))
line.add_yaxis("日本确诊人数", jp_y_data, label_opts=LabelOpts(is_show=False))
line.add_yaxis("印度确诊人数", in_y_data, label_opts=LabelOpts(is_show=False))
line.set_global_opts(
    title_opts=TitleOpts(title="2020年新冠美日印三国确诊人数折线图", pos_left="center", pos_bottom="1%")
)
#  调用render方法，生成折现图表
line.render("2020年新冠美日印三国确诊人数折线图.html")
# 关闭文件对象
f_us.close()
f_jp.close()
f_in.close()
